#ifndef _CTCPSERVER_H_
#define _CTCPSERVER_H_

//#include <direct.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/TCPServer.h>
#include <Poco/Net/TCPServerConnectionFactory.h>
#include <Poco/Net/TCPServerConnection.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Timespan.h>
#include <Poco/Types.h>
#include <sstream>

#include <sys/stat.h>
#include "glog/logging.h"


using Poco::Net::TCPServerConnectionFactory;
using Poco::Net::TCPServerConnection;     //¹¹Ôìº¯ÊýÐèÒª StreamSocket
using Poco::Net::StreamSocket;
using Poco::Net::TCPServer;

const int  MESSAGE_LENGTH = 100;          //¶¨ÒåÐÅÏ¢µÄ³¤¶È

typedef void (*ConnectRun)(StreamSocket); //»Øµ÷

extern ConnectRun g_ConnectRun;

class CZhiTcpServerConnection : public TCPServerConnection
{
public:
	  CZhiTcpServerConnection( const StreamSocket& s):
	  TCPServerConnection( s ){}
	  void run();
};


class CZhiTcpServer : public TCPServerConnectionFactory
{
public:
	inline TCPServerConnection* createConnection(const StreamSocket &socket)
	{
		return new CZhiTcpServerConnection( socket);
	}

};


class CTCPServer
{
private:

	Poco::Net::ServerSocket         *m_ServerSocket;  //socket
	Poco::Net::TCPServerParams::Ptr m_Param ;         //²ÎÊý


public:
	CTCPServer(unsigned short port);                  //³õÊ¼»¯
    ~CTCPServer();                                    //Îö¹¹

	Poco::Net::TCPServer            *m_server;        //tcp·þÎñÆ÷

	void m_setReceiveTimeout(long n);                 //ÉèÖÃ½ÓÊÕµÄ³¬Ê±Ê±¼ä
	void m_setSendTimeout(long n);                    //ÉèÖÃ·¢ËÍµÄ³¬Ê±Ê±¼ä
	void m_setMessageSolve(ConnectRun v_mslove);      //ÉèÖÃÁ¬½Ó´¦Àíº¯Êý

	int  m_getCurrentThreads();                       //·µ»Øµ±Ç°Ê¹ÓÃµÄÁ¬½ÓÏß³ÌµÄÊýÁ¿
	int  m_getMaxThreads();                           //·µ»Ø¿ÉÓÃÏß³ÌµÄ×î´óÊýÁ¿
	int  m_getTotalConnections();                     //·µ»Ø´¦ÀíÁ¬½ÓµÄ×ÜÊý
	int  m_getCurrentConnections();                   //·µ»Øµ±Ç°´¦ÀíµÄÁ¬½ÓÊý
	int  m_getMaxConcurrentConnections();             //·µ»Ø²¢·¢´¦ÀíÁ¬½ÓµÄ×î´óÊýÄ¿
	int  m_getQueuedConnections();                    //·µ»Ø¶ÓÁÐÁ¬½ÓµÄÊýÁ¿
	int  m_getRefusedConnections();                   //·µ»Ø¾Ü¾øÁ¬½ÓµÄÊýÄ¿

};


#endif
